const db = require('../db/index');
const multer = require('multer');
const path = require('path');



exports.roomMessage =  async (req, res) => {
  
  const sql ='select * from rooms'
  db.query(sql,(err,results)=>{
    if (err) {
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}

// 设置文件存储配置
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    // 上传文件保存的目录
    cb(null, 'uploads/');
  },
  filename: (req, file, cb) => {
    // 保留原文件名，也可以根据需要修改文件名
    cb(null, Date.now() + path.extname(file.originalname));
  }
});

// 创建上传对象
//const upload = multer({ storage: storage });
const upload = multer({
  storage: storage,
  limits: {
    fileSize: 10 * 1024 * 1024, // 10MB
  }
});
//上传单个照片用upload.single('file')和req.file，多个照片用upload.array('file')和req.files 
exports.uploadHeadFile = async (req, res) => {
  
  upload.single('file')(req, res, (err) => {
    
    if (err) {
      console.error('Error uploading file:', err); // 打印详细错误信息
      return res.status(500).send('Error uploading file.');
    }
    if (!req.file) {
      console.log(22);
      return res.status(400).send('No file uploaded.');
    }
    // 获取文件的 URL
    const fileUrl = `http://localhost:3008/uploads/${req.file.filename}`;
    
    // 返回成功响应和文件 URL
    return res.status(201).json({ code:0,fileUrl:fileUrl});
  });
};

exports.uploadFiles = async (req, res) => {
  // 使用 upload.array('file') 来处理多个文件
  upload.array('file')(req, res, (err) => {
    if (err) {
      console.error('Error uploading file222:', err); // 打印详细错误信息
      return res.status(500).send('Error uploading file.');
    }

    if (!req.files || req.files.length === 0) {
      return res.status(400).send('No files uploaded.');
    }

    // 返回文件的 URL
    const fileUrls = req.files.map(file => `http://localhost:3008/uploads/${file.filename}`);
    
    // 返回成功响应和文件 URL 列表
    return res.status(201).json({ code: 0, fileUrls: fileUrls });
  });
};

exports.insertRoomData = (req,res)=>{
  const { room_name, room_type, room_img, capacity} = req.body
  const sql ='INSERT INTO rooms (room_name, room_type, room_img, capacity) VALUES (?, ?, ?, ?)';
  db.query(sql,[room_name, room_type, room_img, capacity],(err,results)=>{
    if (err) {
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'rooms registered successfully'});
  })
}

exports.updataRoomData=(req,res)=>{
  const { room_name, room_type, room_img, capacity,room_id} = req.body
  console.log(room_name, room_type, room_img, capacity,room_id);
  const sql =`UPDATE rooms SET room_name = ?, room_type = ?, room_img=?, capacity=? WHERE room_id=${room_id};`;
  db.query(sql,[room_name, room_type, room_img, capacity],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'rooms registered successfully'});
  })
}

exports.delRoomData=(req,res)=>{
  const room_id=req.params.room_id
  console.log(room_id);
  
  const sql=`DELETE FROM rooms WHERE room_id=${room_id};`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'rooms delete successfully'});
  })
}

exports.getRoomImages=(req,res)=>{
  const room_id=req.params.room_id
  const sql =`select room_id,image_url from room_images where room_id=${room_id}`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}
exports.getRoomMessageImg=(req,res)=>{
  const room_id=req.params.room_id
  const sql =`select room_id,image_url from rooms_messageimg where room_id=${room_id}`
  db.query(sql,(err,results)=>{
    if (err) {
      return res.status(500).json({ message: 'Database error' });
    }
    return res.status(201).json({ code:0,data:results});
  })
}

exports.updataRoomData2 = (req,res)=>{
  const { room_name, room_type, price, description, amenities, status, room_id} = req.body
  console.log(room_name, room_type, price, description, amenities, status,room_id);
  const sql =`UPDATE rooms SET room_name = ?, room_type = ?,  price=?, description=?, amenities=?, status=?  WHERE room_id=${room_id};`;
  db.query(sql,[ room_name, room_type, price, description, amenities, status],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'successfully'});
  })
}

exports.insertRoomImages=(req,res)=>{
  const{ room_id,image_url}=req.body
  const sql ='INSERT INTO room_images (room_id,image_url) VALUES (?, ?)';
  db.query(sql,[room_id,image_url],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'successfully'});
  })
}

exports.insertRoomMessageImg=(req,res)=>{
  const {room_id,image_url}=req.body
  const sql ='INSERT INTO rooms_messageimg (room_id,image_url) VALUES (?, ?)';
   db.query(sql,[room_id,image_url],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'successfully'});
  })
}


exports.delRoomData2=(req,res)=>{
  const room_id =req.params.room_id
  const sql = 'UPDATE rooms SET price = ?, description = ?, amenities = ?, status = ? WHERE room_id = ?';
  db.query(sql,[null, '', '', 'available', room_id],(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'successfully'});
  })
}
exports.delRoomImages=(req,res)=>{
  const room_id =req.params.room_id
  const sql=`DELETE FROM room_images WHERE room_id=${room_id};`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'successfully'});
  })
}
exports.delRoomMessageImg=(req,res)=>{
  const room_id =req.params.room_id
  const sql=`DELETE FROM rooms_messageimg WHERE room_id=${room_id};`
  db.query(sql,(err,results)=>{
    if (err) {
      console.log(err);
      return res.status(500).json({ message: 'Database error',err });
    }
    return res.status(201).json({ code:0,message: 'successfully'});
  })
}